<?php

class Dish_Module extends Module{

	function __construct(){
		$this->user   = new AdminUser();
	}

	function filteDish($post, $edit = false) {
		$data = array(
			'res_id'		=> array_get_int($post, 'res_id'),								//餐厅ID
			'dish_cat_id'	=> array_get_int($post, 'dish_cat_id'),							//自定义分类
			'sys_cat_id'	=> array_get_int($post, 'sys_cat_id'),							//系统分类
			'name'			=> array_get_string($post, 'name'),								//菜品名称
			'subname'		=> array_get_string($post, 'subname'),							//菜品附加名称
			'ename'			=> array_get_string($post, 'ename'),							//英文名字
			'price'			=> array_get_double($post, 'price'),							//价格
			'letter'		=> array_get_match($post, 'letter', '/^[a-zA-Z]$/i', ''),		//字母
			'description'	=> array_get_string($post, 'description'),						//菜品描述
			'picture'		=> array_get_string($post, 'picture'),							//图片
		);

		if($edit){
			$data['update_uid']	= $this->user->getId();
			$data['update_dt']	= $this->currentTime();
		}else{
			$data['update_uid']	= $this->user->getId();
			$data['update_dt']	= $this->currentTime();
			$data['create_uid']	= $this->user->getId();
			$data['create_dt']	= $this->currentTime();
		}

		return $data;
	}

	function getStore($res_id){
		$where = array('res_id' => intval($res_id));
		return DB::getOne(DB::getSql('*', 't_store', $where));
	}

	function getDish($res_id, $dish_id){
		$where = array('dish_id' => intval($dish_id), 'res_id' => intval($res_id));
		return DB::getOne(DB::getSql('*', 't_dish', $where));
	}

	function getDishCount($res_id){
		$where = array('res_id' => intval($res_id));
		return DB::getFirst(DB::getSql('count(*)', 't_dish', $where));
	}

	function listDishCat($res_id){
		$where = array('res_id' => intval($res_id));
		$order = 'ordnum ASC, dish_cat_id ASC';
		return DB::getAll(DB::getSql('*', 't_dish_cat', $where, '', $order), null, 'dish_cat_id');
	}

	function listDish($res_id, $search = array(), $limit=20){
		$res_id = intval($res_id);
		$equal = $like = array();
		$where = '';
		if(is_array($search)){
			if(isset($search['dish_cat_id']) && $search['dish_cat_id']){
				$equal['dish_cat_id'] = $search['dish_cat_id'];
			}

			if(isset($search['sys_cat_id']) && $search['sys_cat_id']){
				$equal['sys_cat_id'] = $search['sys_cat_id'];
			}

			if(isset($search['res_id']) && $search['res_id']){
				$equal['res_id'] = $search['res_id'];
			}

			if(isset($search['name']) && $search['name']){
				$like['name'] = $search['name'];
			}

			foreach($equal as $k => $v){
				$where .= " AND `{$k}` = ".DB::quote($v);;
			}
			foreach($like as $k => $v){
				$v = DB::quote($v);
				$where .= " AND `{$k}` Like '%{$v}%'";
			}
		}
		$where  = 'res_id = '.$res_id.$where;

		$limit  = intval($limit);
		$query  = DB::getSql('count(`res_id`)', 't_dish', $where);
		$total  = DB::getFirst($query);

		Base::loadLibrary('Pager');
		$pager  = new Pager($total, $limit);
		$offset = $pager->getOffset();

		$query  = DB::getSql('*', 't_dish', $where, "{$offset}, {$limit}", "dish_cat_id ASC, ordnum ASC");
		$data   = DB::getAll($query);

		$urltpl = Base::adminUrl('dish', 'index').'&res_id='.$res_id.'&page={PAGE}';
		return array($total, $data, $pager->adminPageLink($urltpl));
	}

	function addDish($post){
		DB::insert('t_dish', $this->filteDish($post));
		return DB::lastInsertId();
	}

	function addDishCat($res_id, $cat_name){
		DB::insert('t_dish_cat', array('res_id' => intval($res_id), 'dish_cat_name' => $cat_name));
		return DB::lastInsertId();
	}

	function editDishCatArray($res_id, $cat_arr){
		$stmt = DB::prepare('update `t_dish_cat` set dish_cat_name = ?, ordnum = ? where res_id = ? and dish_cat_id = ?');
		foreach($cat_arr as $cat_id => $cat){
			$array = array(
				$cat['catname'], $cat['ordnum'], intval($res_id), $cat_id
			);
			$stmt->execute($array);
		}
		return true;
	}

	function delDishCatArray($res_id, $ids){
		$stmt = DB::prepare('delete from `t_dish_cat` where res_id = ? and dish_cat_id = ?');
		foreach($ids as $cat_id => $sort){
			$array = array(
				intval($res_id), $cat_id
			);
			$stmt->execute($array);
		}
		return true;
	}

	function importDish($res_id, $array){
		foreach($array as $data){
			Db::insert('t_dish', $this->filteDish($data));
		}
		return true;
	}

	function editDish($res_id, $id, $post){
		$res_id  = intval($res_id);
		$dish_id = intval($id);
		Db::update('t_dish', $this->filteDish($post, true), array('dish_id' => $dish_id));

		//餐厅最后更新时间
		Db::update('t_store', array('update_uid' => $this->user->getId(), 'update_dt' => $this->currentTime()),
						array('res_id' => $res_id));
	}

	function delete($id){
		return $this->db->delete('t_dish', array('dish_id'=>intval($id)));
	}

	function changeArray($ids, $cat_id){
		$cat_id = intval($cat_id);
		foreach($ids as $dish_id){
			$this->db->update('t_dish', array('dish_cat_id'=>$cat_id), array('dish_id'=>$dish_id));
		}
		return true;
	}

	function deleteArray($ids){
		return $this->db->update('t_dish', array('dish_id'=>intval($id)));
	}

	function sortArray($sorts){
		foreach($sorts as $dish_id => $ordnum){
			DB::update('t_dish', array('ordnum'=>$ordnum), array('dish_id'=>$dish_id));
		}
		return true;
	}

	function statusArray($ids, $flag){
		$flag = $flag ? 1 : 0;
		foreach($ids as $dish_id){
			DB::update('t_dish', array('status'=>$flag), array('dish_id'=>$dish_id));
		}
		return true;
	}

}
?>